﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using System.Web.Mail;
using System.Net.Mail;
using System.Net;
using System.IO;

public partial class Main_Email : System.Web.UI.Page
{
    String mailFrom  = "admin@officeonline.com";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        txtFrom.Text = mailFrom;
        this.txtFrom.Enabled = false;
        
    }


    protected void btnSend_Click(object sender, EventArgs e)
    {
        funcMail();
    }

    public void funcMail()
    {
        //MailMessage message = new MailMessage("admin@officeonline.com", "gilbertgardoce@gmail.com");
        
        String mailCc = this.txtCc.Text;
        String mailTo = this.txtTo.Text;
        String mailBody = this.txtBody.Text;
        String mailSubject = this.txtSubject.Text;
        String attachPath = Path.GetTempPath() + uploadedFile.PostedFile.FileName;
        //String from = "gilbertgardoce@gmail.com";
        //String fileExt = System.IO.Path.GetExtension(uploadedFile.PostedFile.FileName);
        //message.CC.Add("destroyer_gilbert@yahoo.com");
        MailMessage message = new MailMessage(mailFrom, mailTo);
        
        if (!mailCc.Trim().Equals(""))
        {
            message.CC.Add(mailCc);
        }
        if (!mailBody.Trim().Equals(""))
        {
            message.Body = mailBody;
        }
        if (!mailSubject.Trim().Equals(""))
        {
            message.Subject = mailSubject;
        }
        message.IsBodyHtml = true;
        //if (!mailSubject.Trim().Equals(""))
        //{
        //    int fileSize = this.uploadedFile.PostedFile.ContentLength;
        //    byte[] fileBinary = new byte[this.uploadedFile.PostedFile.ContentLength];
        //    this.uploadedFile.PostedFile.InputStream.Write(fileBinary, 0, fileSize - 1);
        //    File.WriteAllBytes(Path.GetTempPath() + "/" + this.uploadedFile.PostedFile.FileName, fileBinary);
            
        //    File.WriteAllBytes("", this.uploadedFile.PostedFile.InputStream.

        //   /message.Attachments.Add(new Attachment(uploadedFile.PostedFile.InputStream.Write());
        //}
        if (uploadedFile.HasFile)
        {
            uploadedFile.PostedFile.SaveAs(attachPath);
            if (!mailSubject.Trim().Equals(""))
            {
                message.Attachments.Add(new Attachment(attachPath));
            }
        }

       
        NetworkCredential basicAuthenticationInfo = new NetworkCredential("system@127.0.0.1", "password");
        SmtpClient mailClient = new SmtpClient("localhost");
        mailClient.Port = 25;
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = basicAuthenticationInfo;
        mailClient.Send(message);
        //SmtpMail.SmtpServer = "localhost";
        //SmtpMail.Send(message);
    }
}
